﻿#include "mystring.h"
#include <string.h>
#include <sstream>

std::string TO_STRING(double value)
{
    std::stringstream ss;
    ss <<value;
    return ss.str();
}
using namespace std;
char* MyString::strrev(char* s)
{
    // h指向s的头部
    char* h = s;
    char* t = s;
    char ch;

    // t指向s的尾部
    while (*t++) {};
    t--;    // 与t++抵消
    t--;    // 回跳过结束符'\0'

            // 当h和t未重合时，交换它们所指向的字符
    while (h < t)
    {
        ch = *h;
        *h++ = *t;    // h向尾部移动
        *t-- = ch;    // t向头部移动
    }

    return(s);
}
void MyString::split(const std::string& s, std::string delim, std::vector< std::string >* ret)
{
    size_t last = 0;
    size_t index = s.find_first_of(delim, last);
    while (index != std::string::npos)
    {
        ret->push_back(s.substr(last, index - last));
        last = index + delim.size();
        index = s.find_first_of(delim, last);
    }
    if (index - last > 0)
    {
        ret->push_back(s.substr(last, index - last));
    }
}

//-----------------------------------------------------
//参数：
//chr：要转换的字符
//cint：储存转换过来的数据
//函数功能：字符转换为数据
//-----------------------------------------------------
int MyString::chartoint(unsigned char chr, unsigned char *cint)
{
    unsigned char cTmp;
    cTmp = chr - 48;
    if (cTmp >= 0 && cTmp <= 9)
    {
        *cint = cTmp;
        return 0;
    }
    cTmp = chr - 65;
    if (cTmp >= 0 && cTmp <= 5)
    {
        *cint = (cTmp + 10);
        return 0;
    }
    cTmp = chr - 97;
    if (cTmp >= 0 && cTmp <= 5)
    {
        *cint = (cTmp + 10);
        return 0;
    }
    return 1;
}

//参数：
//str：16进制 两位值 要转换的字符串 {11 12}
//data：储存转换过来的数据串
//len:数据长度
//函数功能：字符串转换为数据串
//-----------------------------------------------------
int MyString::hexstrtodata(unsigned char *str, unsigned char *data, int len, int flag)
{
    unsigned char cTmp = 0;
    int i = 0;
    for (int j = 0;j < len;j++)
    {
        if (chartoint(str[i++], &cTmp))
            return 1;
        data[j] = cTmp;
        if (chartoint(str[i++], &cTmp))
            return 1;
        data[j] = (data[j] << 4) + cTmp;
        if (flag == 1)
            i++;
    }
    return 0;
}

////////////////////////////////////////////////////
//
//功能：二进制取反
//
//输入：const unsigned char *src  二进制数据
//      int length                待转换的二进制数据长度
//
//输出：unsigned char *dst        取反后的二进制数据
//
//返回：0    success
//
//////////////////////////////////////////////////////
int MyString::convert(unsigned char *dst, const unsigned char *src, int length)
{
    int i;
    for (i = 0; i < length; i++)
    {
        dst[i] = src[i] ^ 0xFF;
    }
    return 0;
}
//////////////////////////////////////////////////////////
//
//功能：十六进制转为十进制
//
//输入：const unsigned char *hex         待转换的十六进制数据{0x00,0x11}
//      int length                       十六进制数据长度
//
//输出：
//
//返回：int  rslt                        转换后的十进制数据
//
//思路：十六进制每个字符位所表示的十进制数的范围是0 ~255，进制为256
//      左移8位(<<8)等价乘以256
//
/////////////////////////////////////////////////////////
unsigned long MyString::HextoDec(const unsigned char *hex, int length, bool isBig )
{
    int i;
    unsigned long rslt = 0;
    if (isBig)
    {
        for (i = 0; i < length; i++)
        {
            rslt += (unsigned long)(hex[i]) << (8 * (length - 1 - i));
        }
    }
    else
    {
        for (i = length-1; i >=0; i--)
        {
            rslt += (unsigned long)(hex[length - 1- i]) << (8 * (length - 1 - i));
        }
    }

    return rslt;
}

/////////////////////////////////////////////////////////
//
//功能：十进制转十六进制
//
//输入：int dec                     待转换的十进制数据
//      int length                  转换后的十六进制数据长度
//
//输出：unsigned char *hex          转换后的十六进制数据
//
//返回：0    success
//
//思路：原理同十六进制转十进制
//////////////////////////////////////////////////////////
int MyString::DectoHex(int dec, unsigned char *hex, int length, bool isBig)
{
    if (isBig)
    {
        for (int i = length - 1; i >= 0; i--)
        {
            hex[i] = (dec % 256) & 0xFF;
            dec /= 256;
        }
    }
    else
    {
        int startIndex = length - 1;
        for (int i = startIndex; i >= 0; i--)
        {
            hex[startIndex - i] = (dec % 256) & 0xFF;
            dec /= 256;
        }
    }

    return 0;
}
/////////////////////////////////////////////////////////
//
//功能：求权
//
//输入：int base                    进制基数
//      int times                   权级数
//
//输出：
//
//返回：unsigned long               当前数据位的权
//
//////////////////////////////////////////////////////////
unsigned long MyString::power(int base, int times)
{
    int i;
    unsigned long rslt = 1;
    for (i = 0; i < times; i++)
        rslt *= base;
    return rslt;
}
/////////////////////////////////////////////////////////
//
//功能：BCD转10进制
//
//输入：const unsigned char *bcd     待转换的BCD码
//      int length                   BCD码数据长度
//
//输出：
//
//返回：unsigned long               当前数据位的权
//
//思路：压缩BCD码一个字符所表示的十进制数据范围为0 ~ 99,进制为100
//      先求每个字符所表示的十进制值，然后乘以权
//////////////////////////////////////////////////////////
unsigned long  MyString::BCDtoDec(const unsigned char *bcd, int length)
{
    int i, tmp;
    unsigned long dec = 0;
    for (i = 0; i < length; i++)
    {
        tmp = ((bcd[i] >> 4) & 0x0F) * 10 + (bcd[i] & 0x0F);
        dec += tmp * power(100, length - 1 - i);
    }
    return dec;
}
/////////////////////////////////////////////////////////
//
//功能：十进制转BCD码
//
//输入：int Dec                      待转换的十进制数据
//      int length                   BCD码数据长度
//
//输出：unsigned char *Bcd           转换后的BCD码
//
//返回：0  success
//
//思路：原理同BCD码转十进制
//
//////////////////////////////////////////////////////////
int MyString::DectoBCD(int Dec, unsigned char *Bcd, int length)
{
    int i;
    int temp;
    for (i = length - 1; i >= 0; i--)
    {
        temp = Dec % 100;
        Bcd[i] = ((temp / 10) << 4) + ((temp % 10) & 0x0F);
        Dec /= 100;
    }
    return 0;
}

void MyString::convertUnCharToChar(unsigned char* UnChar, int ucLen, char* str)
{
    int i = 0;
    for (i = 0; i < ucLen; i++)
    {
        //格式化输str,每unsigned char 转换字符占两位置%x写输%X写输
        sprintf(str + i * 2, "%02x", UnChar[i]);
    }
}

void MyString::convertCharToUnChar(char* str, unsigned char* UnChar)
{
    int i = strlen(str);
    int j = 0, counter = 0;
    char c[2];
    unsigned int bytes[2];

    for (j = 0; j < i; j += 2)
    {
        if (0 == j % 2)
        {
            c[0] = str[j];
            c[1] = str[j + 1];
            sscanf(c, "%02x", &bytes[0]);
            UnChar[counter] = bytes[0];
            counter++;
        }
    }
    return;
}

std::string MyString::convert_hex(unsigned char *md, int nLen)
{

    static const char hex_chars[] = "0123456789ABCDEF";
    std::string res;

    unsigned int c = 0;

    // 查看unsigned char占几个字节
    // 实际占1个字节，8位
    int nByte = sizeof(unsigned char);

    for (int i = 0; i < nLen; i++)
    {
        // 查看md一个字节里的信息
        unsigned int x = 0;
        x = md[i];
        x = md[i] >> 4;// 右移，干掉4位，左边高位补0000

        c = (md[i] >> 4) & 0x0f;
        res += hex_chars[c];
        res += hex_chars[md[i] & 0x0f];
    }
    return res;
}
// 大小端转换
unsigned int MyString::reversebytes_uint32t(unsigned int value) {
    return (value & 0x000000FFU) << 24 | (value & 0x0000FF00U) << 8 |
        (value & 0x00FF0000U) >> 8 | (value & 0xFF000000U) >> 24;
}
#ifdef  WIN32
//需要外部删除返回结果
char *MyString::wchar2char(const wchar_t* wchar)
{
    char * m_char;
    int len = WideCharToMultiByte(CP_ACP, 0, wchar, wcslen(wchar), NULL, 0, NULL, NULL);
    m_char = new char[len + 1];
    WideCharToMultiByte(CP_ACP, 0, wchar, wcslen(wchar), m_char, len, NULL, NULL);
    m_char[len] = '\0';
    return m_char;
}
//需要外部删除返回结果
wchar_t * MyString::char2wchar(const char* cchar)
{
    wchar_t *m_wchar;
    int len = MultiByteToWideChar(CP_ACP, 0, cchar, strlen(cchar), NULL, 0);
    m_wchar = new wchar_t[2048];
    memset(m_wchar, 0, 2048);
    MultiByteToWideChar(CP_ACP, 0, cchar, strlen(cchar), m_wchar, len);
    m_wchar[len] = '\0';
    return m_wchar;
}
#endif //  WIN32


void MyString::vector_string_to_vector_double(const std::vector<std::string>& in, std::vector<double> &out)
{
    for (const string& t : in)
    {
        out.push_back(atof(t.c_str()));
    }
}
void MyString::vector_double_to_vector_string(const std::vector<double> &in, std::vector<string>& out)
{
    for (const double& t : in)
    {
        out.push_back(TO_STRING(t));
    }
}
std::string MyString::vector_double_to_string(const std::vector<double> &in, const std::string &split)
{
    std::string out;
    for (const double& t : in)
    {
        out += TO_STRING(t) + split;
    }
    return out;
}

std::string MyString::vector_string_to_string(const std::vector<std::string> &in, const std::string &split)
{
    std::string out;
    for (const std::string& t : in)
    {
        out += t + split;
    }
    return out;
}
void MyString::string_replace(std::string &strBig, const std::string &strsrc, const std::string &strdst)
{
    std::string::size_type pos = 0;
    std::string::size_type srclen = strsrc.size();
    std::string::size_type dstlen = strdst.size();

    while ((pos = strBig.find(strsrc, pos)) != std::string::npos)
    {
        strBig.replace(pos, srclen, strdst);
        pos += dstlen;
    }
}
